import SystemApi from '@/api/SystemApi'
import BizDictApi from '@/api/basebiz/BizDictApi'
import i18n from '@/i18n'
import store from '..'
import { deepClone } from '@/utils/common'
/**
 * 字典
 */
export default {
  state: {
    allNodeTypes: [],
    dict: null,
    dictList: {},
    dictMap: {},
    cableLevelData: null,
    customDictList: [],
    customDictMap: {}
  },
  getters: {
    allNodeTypes: state => state.allNodeTypes, // 获取字典
    getDict: state => state.dict, // 获取字典
    dictMap: state => state.dictMap, // 获取字典MAP
    dictList: state => state.dictList, // 获取字典列表
    customDictList: state => state.customDictList,
    customDictMap: state => state.customDictMap
  },
  actions: {
    loadDict: ({ dispatch, commit }) =>
      SystemApi.dict()
        .then(dict => {
          // 告警配置
          dispatch('getConfigData', true)
          commit('setDict', deepClone(dict))
        })
        .catch(err => {
          console.log(err)
        }), // 加载字典数据
    getCustomDict: ({ commit }) =>
      BizDictApi.getAll()
        .then(res => {
          commit('setCustomDict', res || [])
        })
        .catch(err => {
          console.log(err)
        })
  },
  mutations: {
    setDict(state, dict) {
      dict = dict || state.dict
      if (!dict) return
      store.state.i18n.downloaded && (dict = transition(dict))
      // 设置字典数据
      state.dict = dict
      const allNodeTypes = dict[dict.findIndex(item => item.code === 'NODE_TYPE')].children
      allNodeTypes.forEach(i => {
        i.nodeCategory = getNodeCategory(i.code)
      })
      state.allNodeTypes = allNodeTypes.filter(item => item.code !== 'SPLICE_CLOSURE')
      state.dictList = {}
      state.dictMap = {}
      // 循环封装字典数据
      dict.forEach(({ code, children }) => {
        state.dictList[code] = []
        state.dictMap[code] = {}
        // 封装字典子集数据
        if (children && children.length) {
          // 封装中文字典子集数据
          state.dictList[code] = children.map(({ code, name }) => ({
            label: name,
            value: code
          }))
          const cnDm = {}
          children.forEach(({ code, name }) => (cnDm[code] = name))
          state.dictMap[code] = cnDm
        }
      })
    },
    setCustomDict(state, data) {
      // 自定义字典
      if (!data) return
      const customDictList = []
      const customDictMap = {}
      data.forEach(i => {
        const item = {
          ...i,
          label: i.name,
          value: i.code
        }
        customDictList.push(item)
        customDictMap[i.code] = i.name
      })
      customDictList.sort((a, b) => a.sort - b.sort)
      state.customDictList = customDictList
      state.customDictMap = customDictMap
    }
  }
}
// 多语言转换
function transition(data, parnetCode) {
  parnetCode = parnetCode ? parnetCode + '_' : ''
  data.forEach(i => {
    i.name = i18n.t(parnetCode + i.code)
    i.label = i18n.t(parnetCode + i.code)
    if (i.children && i.children.length) {
      i.children = transition(i.children, parnetCode + i.code)
    }
  })
  return data
}
// 获取nodeCategory
function getNodeCategory(nodeType) {
  const nodeCategoryArr = {
    AFS: ['AFS', 'CLUSTER', 'AOCC'],
    ODF: ['ODF', 'LIGHT_DELIVERY'],
    TRANSPORT: ['OTN', 'SDH', 'PTN', 'OLT', 'PDH', 'ASON', 'WDM', 'EXCHANGE', 'ROUTER'],
    LIGHT_SPLIT: ['LIGHT_SPLIT'],
    SPLICE_CLOSURE: ['SPLICE_CLOSURE'] // 接头盒
  }
  let nodeCategory = ''
  Object.keys(nodeCategoryArr).forEach(i => {
    if (nodeCategoryArr[i].includes(nodeType)) {
      nodeCategory = i
    }
  })
  return nodeCategory
}
